package zd.cornermemory.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zd.cornermemory.R;
import zd.cornermemory.utils.SPUtils;
import zd.cornermemory.utils.SpKey;

/**
 * 修改读码的打乱公式生成规则
 */
public class MotifyReadActivity extends BaseActivity {
    private Spinner read_type;
    private Button save_button;
    private int readType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_motify_read);
        initView();
        initEvent();
        int type = SPUtils.getInstance().getInt(SpKey.READ_TYPE,0);
        read_type.setSelection(type);
        setBackImage();
    }

    private void initEvent() {

        read_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                readType = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        save_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SPUtils.getInstance().put(SpKey.READ_TYPE,readType);
                Toast.makeText(mContext,getString(R.string.motify_sucs),Toast.LENGTH_SHORT).show();
                Intent broadcast = new Intent(SpKey.MODIFY_READ_SUCCESS);
                sendBroadcast(broadcast);
                finish();
            }
        });
    }

    private void initView() {
        read_type = findViewById(R.id.read_type);
        save_button = findViewById(R.id.save_button);
        String[] cornerBuffer = getResources().getStringArray(R.array.read_type);
        List<String> cornerList = new ArrayList<>();
        for(int i=0;i<cornerBuffer.length;i++) {
            cornerList.add(cornerBuffer[i]);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,

                R.layout.spinner_checked_text, cornerList);
        read_type.setAdapter(adapter);

    }

}
